package com.rivulet.fileserver.service.impl;

import com.rivulet.fileserver.mapper.FileMapper;
import com.rivulet.fileserver.pojo.FileData;
import com.rivulet.fileserver.service.FileService;
import com.rivulet.fileserver.tools.Results;
import org.springframework.stereotype.Service;

@Service
public class FileServiceImpl implements FileService {
    final private FileMapper fileMapper;

    public FileServiceImpl(FileMapper fileMapper) {
        this.fileMapper = fileMapper;
    }

    @Override
    public Results saveFileData(FileData fileData) {
        Results<Object> results=new Results(100,null);
        try {
            fileMapper.setFileData(
                    fileData.getUuid(),
                    fileData.getFile_name(),
                    fileData.getFile_size(),
                    fileData.getFile_type(),
                    fileData.getFile_create_time(),
                    fileData.getFile_save_path()
            );
        }catch (Exception e){
            e.printStackTrace();
            results.setCode(400);
        }

        return results;
    }

    @Override
    public Results getFileData(String uuid) {
        Results<FileData> results=new Results<>(410,null);
        try {
            FileData fileData=fileMapper.getFileDataByUUID(uuid);
            if(fileData!=null){
                results.setData(fileData);
                results.setCode(100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return results;
    }
}
